package com.zhao.modleclass.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.zhao.modleclass.R;
import com.zhao.modleclass.bean.HP_NewBean;
import com.zhao.modleclass.bean.HomePageBean;
import com.zhao.modleclass.interfaces.IViewPager;
import com.zhao.modleclass.utils.Constant;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HPType1Activity extends Activity {

    @Bind(R.id.iv_Toleft_AcT1)
    ImageView ivToleftAcT1;
    @Bind(R.id.iv_Tohome_AcT1)
    ImageView ivTohomeAcT1;
    @Bind(R.id.tv_title_AcT1)
    TextView tvTitleAcT1;
    @Bind(R.id.videoView_title_AcT1)
    VideoView videoViewTitleAcT1;
    @Bind(R.id.tv_playConuts_AcT1)
    TextView tvPlayConutsAcT1;
    @Bind(R.id.iv_play_AcT1)
    ImageView ivPlayAcT1;
    @Bind(R.id.iv_show_AcT1)
    ImageView ivShowAcT1;
    @Bind(R.id.tv_plattimes_AcT1)
    TextView tvPlattimesAcT1;
    @Bind(R.id.wb_hp_type1)
    WebView wbHpType1;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hp_type1);
        //设置状态栏的颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //获取样式中的属性值
            TypedValue typedValue = new TypedValue();
            this.getTheme().resolveAttribute(Color.parseColor("#FF4081"), typedValue, true);
            int[] attribute = new int[]{Color.parseColor("#FF4081")};
            TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);
            int color = array.getColor(0, Color.parseColor("#FF4081"));
            array.recycle();
            window.setStatusBarColor(color);
        }
        ButterKnife.bind(this);
        Intent intent = getIntent();
        int type = intent.getIntExtra("Type", 0);
        //判断是否来自banner的跳转type=1则证明是
        if (type == 1) {
            Bundle bundle = intent.getBundleExtra("banner");
            HomePageBean.FocusBean banner = (HomePageBean.FocusBean) bundle.getSerializable("banner");
            tvTitleAcT1.setText(banner.getTitle());
            tvPlayConutsAcT1.setText(banner.getContentId() + "");
            Glide.with(this).load(banner.getImageUrl()).into(ivShowAcT1);

            final int CourseId = intent.getIntExtra("CourseId", 0);
            Map<String, String> map = new HashMap<>();
            map.put("courseId", CourseId + "");
            map.put("deviceId", "7fb8fd0b59f6358e965b628fae11306e");
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constant.URL_HP_NEW)
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            IViewPager iViewPager = retrofit.create(IViewPager.class);
            Observable<HP_NewBean> observable = iViewPager.getHP_NewBean(map);
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<HP_NewBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(final HP_NewBean hp_newBean) {
                            tvTitleAcT1.setText(hp_newBean.getData().getTitle());
                            tvPlayConutsAcT1.setText(hp_newBean.getData().getTotalTime());
                            Glide.with(HPType1Activity.this).load(hp_newBean.getData().getImageUrl()).into(ivShowAcT1);
                            ivPlayAcT1.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ivPlayAcT1.setVisibility(View.INVISIBLE);
                                    ivShowAcT1.setVisibility(View.INVISIBLE);
                                    tvPlayConutsAcT1.setVisibility(View.INVISIBLE);
                                    tvPlattimesAcT1.setVisibility(View.INVISIBLE);
                                    videoViewTitleAcT1.setMediaController(new MediaController(HPType1Activity.this));
                                    videoViewTitleAcT1.setVideoURI(Uri.parse(hp_newBean.getData().getCourseUrl().get(0)));
                                    videoViewTitleAcT1.requestFocus();

                                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                                            RelativeLayout.LayoutParams.MATCH_PARENT,
                                            RelativeLayout.LayoutParams.MATCH_PARENT);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                                    //点击播放视频
                                    videoViewTitleAcT1.setLayoutParams(layoutParams);
                                    videoViewTitleAcT1.setVisibility(View.VISIBLE);
                                    videoViewTitleAcT1.start();
                                }
                            });
                        }
                    });
            //https://mrobot.pclady.com.cn/modern/s/course/videoCourseInfo.xsp?courseId=1307&deviceId=4024b89134ec3dd6818f09e422d2cafa
            //https://mrobot.pclady.com.cn/modern/s/live/liveCourse.xsp?
            // courseId=1294
            // &deviceId=4024b89134ec3dd6818f09e422d2cafa&inReview=0&FromPCapp=PCLADY_MODERN_ANDROID_1.0.0&appVersion=1220
            final String url = "https://mrobot.pclady.com.cn/modern/s/course/videoCourseInfo.xsp?courseId=" + CourseId +
                    "&deviceId=4024b89134ec3dd6818f09e422d2cafa";
            wbHpType1.loadUrl(url);
            //开启javaScript支持
            wbHpType1.getSettings().setJavaScriptEnabled(true);
            //强制使用控件加载网页
            wbHpType1.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    view.loadUrl(url);
                    return true;
                }
            });
        } else if (type == 3) {
            //Type=3表示是从最新课程跳转过来
            final int CourseId = intent.getIntExtra("CourseId", 0);
            String title=intent.getStringExtra("title");
            String imag_URL=intent.getStringExtra("imag_URL");

            tvTitleAcT1.setText(title);
            Glide.with(this).load(imag_URL).into(ivShowAcT1);


            Map<String, String> map = new HashMap<>();
            map.put("courseId", CourseId + "");
            map.put("deviceId", "7fb8fd0b59f6358e965b628fae11306e");
            //4024b89134ec3dd6818f09e422d2cafa
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constant.URL_HP_NEW)
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            IViewPager iViewPager = retrofit.create(IViewPager.class);
            Observable<HP_NewBean> observable = iViewPager.getHP_NewBean(map);
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<HP_NewBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.i("TAG", "onError: "+e.toString());
                        }

                        @Override
                        public void onNext(final HP_NewBean hp_newBean) {
                            tvTitleAcT1.setText(hp_newBean.getData().getTitle());
                            tvPlayConutsAcT1.setText(hp_newBean.getData().getTotalTime());
                            Glide.with(HPType1Activity.this).load(hp_newBean.getData().getImageUrl()).into(ivShowAcT1);
                            ivPlayAcT1.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ivPlayAcT1.setVisibility(View.INVISIBLE);
                                    ivShowAcT1.setVisibility(View.INVISIBLE);
                                    tvPlayConutsAcT1.setVisibility(View.INVISIBLE);
                                    tvPlattimesAcT1.setVisibility(View.INVISIBLE);
                                    videoViewTitleAcT1.setMediaController(new MediaController(HPType1Activity.this));
                                    videoViewTitleAcT1.setVideoURI(Uri.parse(hp_newBean.getData().getCourseUrl().get(0)));
                                    videoViewTitleAcT1.requestFocus();

                                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                                            RelativeLayout.LayoutParams.MATCH_PARENT,
                                            RelativeLayout.LayoutParams.MATCH_PARENT);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                                    //点击播放视频
                                    videoViewTitleAcT1.setLayoutParams(layoutParams);
                                    videoViewTitleAcT1.setVisibility(View.VISIBLE);
                                    videoViewTitleAcT1.start();
                                }
                            });
                        }
                    });
            //https://mrobot.pclady.com.cn/modern/s/course/videoCourseInfo.xsp?courseId=1307&deviceId=4024b89134ec3dd6818f09e422d2cafa
            //https://mrobot.pclady.com.cn/modern/s/live/liveCourse.xsp?
            // courseId=1294
            // &deviceId=4024b89134ec3dd6818f09e422d2cafa&inReview=0&FromPCapp=PCLADY_MODERN_ANDROID_1.0.0&appVersion=1220
            final String url = "https://mrobot.pclady.com.cn/modern/s/course/videoCourseInfo.xsp?courseId=" + CourseId +
                    "&deviceId=4024b89134ec3dd6818f09e422d2cafa";
            wbHpType1.loadUrl(url);
            //开启javaScript支持
            wbHpType1.getSettings().setJavaScriptEnabled(true);
            //强制使用控件加载网页
            wbHpType1.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    view.loadUrl(url);
                    return true;
                }
            });
        }


        ivToleftAcT1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivTohomeAcT1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
